<?php
/**
 * (SuperAdmin)
 * List all users and can edit information & permission
 */
class user extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('url');
        if (file_exists('application/controllers/install.php'))
            redirect('install');
        $this->load->library('session');
        $this->load->model('user_model');

        if (!($this->session->userdata['login'])) {
            redirect('poster_admin/login');
        } elseif ($this->session->userdata['level'] != 99) {    // Check the right
            $data['title'] = '出错啦';
            $data['words'] = '您无权进行本操作';
            $this->load->view('templates/poster_admin/header', $data);
            $this->load->view('poster_admin/404', $data);
            $this->load->view('templates/poster_admin/footer', $data);
            die();
        }
    }

    function index() {
        $data['title'] = '用户列表';
        $data['query'] = $this->user_model->getAll();

        $this->load->view('templates/poster_admin/header', $data);
        $this->load->view('poster_admin/user/index', $data);
        $this->load->view('templates/poster_admin/footer', $data);

    }

    function create() {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('username', '用户名', 'trim|min_length[5]|max_length[16]|required|xss_clean|is_unique[pr_users.username]|alpha_dash');
        $this->form_validation->set_rules('password', '密码', 'trim|required|min_length[6]|max_length[20]|matches[passconf]|md5');
        $this->form_validation->set_rules('passconf', '确认密码', 'trim|required');

        $data['title'] = '创建新用户';

        if ($this->form_validation->run() === FALSE) {
            $this->load->view('templates/poster_admin/header', $data);
            $this->load->view('poster_admin/user/create', $data);
            $this->load->view('templates/poster_admin/footer', $data);
        } else {
            $level = 5;
            $this->user_model->insert($level);
            redirect('poster_admin/user/');
        }
    }

    function edit() {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('password', '密码', 'trim|min_length[6]|max_length[20]|md5');

        $data['query'] = $this->user_model->select();
        if (empty($data['query'])) { // if uid not existed, redirect
            $data['title'] = '出错啦';
            $data['words'] = '查询id错误';
            $this->load->view('templates/poster_admin/header', $data);
            $this->load->view('poster_admin/404', $data);
            $this->load->view('templates/poster_admin/footer', $data);
        } else {
            $data['title'] = '编辑用户';

            if ($this->form_validation->run() === FALSE) {
                $this->load->view('templates/poster_admin/header', $data);
                $this->load->view('poster_admin/user/edit', $data);
                $this->load->view('templates/poster_admin/footer', $data);
            } else {
                $password = $this->input->post('password');
                if (!empty($password))
                    $this->user_model->edit();
                redirect('poster_admin/user/');
            }
        }
    }

    function delete() {
        $this->user_model->delete();
        redirect('poster_admin/user/');
    }
}